package com.jinmdz.fmis.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 驼峰命名法工具类
 * (源自网络)
 *
 * @author LiCongLu
 * @date 2019-12-13 14:42
 */
public final class CamelCaseUtil {
    public static final char UNDERLINE = '_';

    private static Pattern pattern = Pattern.compile("_");

    /**
     * 驼峰格式字符串转换为下划线格式字符串
     * 源数据为空时，返回空字符串
     *
     * @param source 源数据
     * @return
     */
    public static String camelToUnderline(String source) {
        if (source == null || source.trim().length() == 0) {
            return "";
        }
        int len = source.length();
        StringBuilder builder = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = source.charAt(i);
            if (Character.isUpperCase(c)) {
                builder.append(UNDERLINE);
                builder.append(Character.toLowerCase(c));
            } else {
                builder.append(c);
            }
        }
        return builder.toString();
    }

    /**
     * 下划线格式字符串转换为驼峰格式字符串
     * 源数据为空时，返回空字符串
     *
     * @param source 源数据
     * @return
     */
    public static String underlineToCamel(String source) {
        if (source == null || source.trim().length() == 0) {
            return "";
        }
        int len = source.length();
        StringBuilder builder = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = source.charAt(i);
            if (c == UNDERLINE) {
                if (++i < len) {
                    builder.append(Character.toUpperCase(source.charAt(i)));
                }
            } else {
                builder.append(c);
            }
        }
        return builder.toString();
    }

    /**
     * 下划线格式字符串转换为驼峰格式字符串2
     * 源数据为空时，返回空字符串
     *
     * @param source 源数据
     * @return
     */
    public static String underlineToCamel2(String source) {
        if (source == null || source.trim().length() == 0) {
            return "";
        }
        StringBuilder builder = new StringBuilder(source);
        Matcher mc = pattern.matcher(source);
        int i = 0;
        while (mc.find()) {
            int position = mc.end() - (i++);
            builder.replace(position - 1, position + 1, builder.substring(position, position + 1).toUpperCase());
        }
        return builder.toString();
    }

    /**
     * 首字母大写方法一
     * 默认返回源数据
     *
     * @param source 源数据
     * @return
     */
    public static String upperFirstLetter(String source) {
        if (source != null && source.length() > 0) {
            char a = 'a';
            char z = 'z';
            char[] chars = source.toCharArray();
            if (chars[0] >= a && chars[0] <= z) {
                chars[0] = (char) (chars[0] - 32);
            }
            return new String(chars);
        }
        return source;
    }

    /**
     * 首字母大写方法二
     * 默认返回源数据
     *
     * @param source 源数据
     * @return
     */
    public static String upperFirstLetter2(String source) {
        if (source != null && source.length() > 0) {
            return source.substring(0, 1).toUpperCase() + source.substring(1);
        }
        return source;
    }

    public static void main(String[] args) {
        String str = "operation_no";
        System.out.println(upperFirstLetter(str));
        System.out.println(upperFirstLetter2(str));
        str = "graveSaleId";
        System.out.println(upperFirstLetter2(str));

    }
}
